package com.bthdung.studios.lesson12.framework.buffers;

import java.nio.FloatBuffer;

import com.bthdung.studios.lesson12.framework.VertexPositionTextureNormal;

import android.opengl.GLES20;


public class VertexBuffer
{
	int bufferID = -1;
	FloatBuffer buffer = null;

	public VertexBuffer(VertexPositionTextureNormal[] verts)
	{
		this(getFloatArray(verts));
	}

	public VertexBuffer(float[] data)
	{
		buffer = BufferUtils.createFloatBuffer(data);
		bufferID = BufferUtils.glGenerateVertexBuffer(buffer);
	}

	public void glBind()
	{
		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferID);
	}

	static float[] getFloatArray(VertexPositionTextureNormal[] verts)
	{
		int elementsCount = VertexPositionTextureNormal.ELEMENTS_COUNT;
		float[] data = new float[verts.length * elementsCount];

		for (int i = 0; i < verts.length; i++)
			verts[i].copyElements(data, i * elementsCount);

		return data;
	}

	public int glGetID()
	{
		return bufferID;
	}
}
